using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class SearchBook : System.Web.UI.UserControl
{
    int libID;
    string code;
    Book book;
    Library library;
    Publisher publisher;
    CheckedOutBook coBook;
    ReservedBook reservedBook;
    ManageReservedBook manageReservedBook;
    ManageBook manageBook;
    ManageCheckedOutBook manageCheckedOutBook;
    ManageLibrary manageLibrary;
    ManagePublisher managePublisher;
    Patron patron;
    UtilityManager utilManager;

    protected void Page_Load(object sender, EventArgs e)
    {
        //needed variables for book details - Elizabeth Dudding
        libID = Convert.ToInt32(Request.QueryString["id"]);
        manageBook = new ManageBook();
        manageCheckedOutBook = new ManageCheckedOutBook();
        manageLibrary = new ManageLibrary();
        managePublisher = new ManagePublisher();
        manageReservedBook = new ManageReservedBook();
        book = new Book();
        library = new Library();
        publisher = new Publisher();
        coBook = new CheckedOutBook();
        reservedBook = new ReservedBook();
        utilManager = new UtilityManager();
        pnlBookDetails.Visible = false;
        patron = (Patron)Session["Patron"];
        if (patron == null)
        {
            patron = new Patron();
        }
    }


    protected void btnISBN_Click(object sender, EventArgs e)
    {
        // Set the appropriate tab active
        btnKeyword.CssClass = "notSelected";
        btnAuthor.CssClass = "notSelected";
        btnISBN.CssClass = "selected";
        btnSubject.CssClass = "notSelected";
        btnTitle.CssClass = "notSelected";

        // Set appropriate panel to show -- change to for each statement
        pnlauthor.CssClass = "hide";
        pnlISBN.CssClass = "panel";
        pnlkeyword.CssClass = "hide";
        pnlsubject.CssClass = "hide";
        pnltitle.CssClass = "hide";


    }
    protected void btnKeyword_Click(object sender, EventArgs e)
    {
        // Set the appropriate tab active
        btnKeyword.CssClass = "selected";
        btnAuthor.CssClass = "notSelected";
        btnISBN.CssClass = "notSelected";
        btnSubject.CssClass = "notSelected";
        btnTitle.CssClass = "notSelected";

        // Set appropriate panel to show -- change to for each statement
        pnlauthor.CssClass = "hide";
        pnlISBN.CssClass = "hide";
        pnlkeyword.CssClass = "panel";
        pnlsubject.CssClass = "hide";
        pnltitle.CssClass = "hide";
    }
    protected void btnTitle_Click(object sender, EventArgs e)
    {
        // Set the appropriate tab active
        btnKeyword.CssClass = "notSelected";
        btnAuthor.CssClass = "notSelected";
        btnISBN.CssClass = "notSelected";
        btnSubject.CssClass = "notSelected";
        btnTitle.CssClass = "selected";

        // Set appropriate panel to show -- change to for each statement
        pnlauthor.CssClass = "hide";
        pnlISBN.CssClass = "hide";
        pnlkeyword.CssClass = "hide";
        pnlsubject.CssClass = "hide";
        pnltitle.CssClass = "panel";
    }
    protected void btnAuthor_Click(object sender, EventArgs e)
    {
        // Set the appropriate tab active
        btnKeyword.CssClass = "notSelected";
        btnAuthor.CssClass = "selected";
        btnISBN.CssClass = "notSelected";
        btnSubject.CssClass = "notSelected";
        btnTitle.CssClass = "notSelected";

        // Set appropriate panel to show -- change to for each statement
        pnlauthor.CssClass = "panel";
        pnlISBN.CssClass = "hide";
        pnlkeyword.CssClass = "hide";
        pnlsubject.CssClass = "hide";
        pnltitle.CssClass = "hide";
    }
    protected void btnSubject_Click(object sender, EventArgs e)
    {
        // Set the appropriate tab active
        btnKeyword.CssClass = "notSelected";
        btnAuthor.CssClass = "notSelected";
        btnISBN.CssClass = "notSelected";
        btnSubject.CssClass = "selected";
        btnTitle.CssClass = "notSelected";

        // Set appropriate panel to show -- change to for each statement
        pnlauthor.CssClass = "hide";
        pnlISBN.CssClass = "hide";
        pnlkeyword.CssClass = "hide";
        pnlsubject.CssClass = "panel";
        pnltitle.CssClass = "hide";
    }

    protected void btnTitleSubmit_Click(object sender, EventArgs e)
    {
        ManageBook manager = new ManageBook();

        SqlDataAdapter dat = manager.SearchByTitle(txtTitle.Text);
        DataSet d = new DataSet();
        dat.Fill(d);

        gvResultsByTitle.DataSource = d;
        gvResultsByTitle.DataBind();

        if (gvResultsByTitle.Rows.Count<1)
        {
            LblNoBooksTitle.Visible = true;
        }
        else
            LblNoBooksTitle.Visible = false;
    }
    protected void btnKeywordSubmit_Click(object sender, EventArgs e)
    {
        ManageBook manager = new ManageBook();

        SqlDataAdapter dat = manager.SearchByKeyword(txtKeyword.Text);
        DataSet d = new DataSet();
        dat.Fill(d);

        gvResultsByKeyword.DataSource = d;
        gvResultsByKeyword.DataBind();
        
        if (gvResultsByKeyword.Rows.Count<1)
        {
            LblNoBooksKeyword.Visible = true;
        }
        else
            LblNoBooksKeyword.Visible = false;
    }
    protected void btnAuthorSubmit_Click(object sender, EventArgs e)
    {
        ManageBook manager = new ManageBook();

        SqlDataAdapter dat = manager.SearchByAuthor(txtAuthor.Text);
        DataSet d = new DataSet();
        dat.Fill(d);

        gvResultsByAuthor.DataSource = d;
        gvResultsByAuthor.DataBind();

        if (gvResultsByAuthor.Rows.Count<1)
        {
            LblNoBooksAuthor.Visible = true;
        }
        else
            LblNoBooksAuthor.Visible = false;
    }
    protected void btnSubjectSubmit_Click(object sender, EventArgs e)
    {
        ManageBook manager = new ManageBook();

        SqlDataAdapter dat = manager.SearchBySubject(txtSubject.Text);
        DataSet d = new DataSet();
        dat.Fill(d);

        gvResultsBySubject.DataSource = d;
        gvResultsBySubject.DataBind();

        if (gvResultsBySubject.Rows.Count<1)
        {
            LblNoBooksSubject.Visible = true;
        }
        else
            LblNoBooksSubject.Visible = false;
    }
    protected void btnISBNSubmit_Click(object sender, EventArgs e)
    {
        ManageBook manager = new ManageBook();

        SqlDataAdapter dat = manager.SearchByISBN(txtISBN.Text);
        DataSet d = new DataSet();
        dat.Fill(d);

        gvResultsByISBN.DataSource = d;
        gvResultsByISBN.DataBind();        

        if (gvResultsByISBN.Rows.Count<1)
        {
            LblNoBooksISBN.Visible = true;
        }
        else
            LblNoBooksISBN.Visible = false;
    }
    
    protected void gvResultsByKeyword_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ManageBook manager = new ManageBook();

        SqlDataAdapter dat = manager.SearchByKeyword(txtKeyword.Text);
        DataSet d = new DataSet();
        dat.Fill(d);

        gvResultsByKeyword.DataSource = d;

        gvResultsByKeyword.PageIndex = e.NewPageIndex;
        gvResultsByKeyword.DataBind();
    }
    protected void gvResultsByTitle_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ManageBook manager = new ManageBook();

        SqlDataAdapter dat = manager.SearchByTitle(txtTitle.Text);
        DataSet d = new DataSet();
        dat.Fill(d);

        gvResultsByTitle.DataSource = d;

        gvResultsByTitle.PageIndex = e.NewPageIndex;
        gvResultsByTitle.DataBind();
    }
    protected void gvResultsByAuthor_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ManageBook manager = new ManageBook();

        SqlDataAdapter dat = manager.SearchByAuthor(txtAuthor.Text);
        DataSet d = new DataSet();
        dat.Fill(d);

        gvResultsByAuthor.DataSource = d;

        gvResultsByAuthor.PageIndex = e.NewPageIndex;
        gvResultsByAuthor.DataBind();
    }
    protected void gvResultsBySubject_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ManageBook manager = new ManageBook();

        SqlDataAdapter dat = manager.SearchBySubject(txtSubject.Text);
        DataSet d = new DataSet();
        dat.Fill(d);

        gvResultsBySubject.DataSource = d;

        gvResultsBySubject.PageIndex = e.NewPageIndex;
        gvResultsBySubject.DataBind();
    }
    protected void gvResultsByISBN_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ManageBook manager = new ManageBook();

        SqlDataAdapter dat = manager.SearchByISBN(txtISBN.Text);
        DataSet d = new DataSet();
        dat.Fill(d);

        gvResultsByISBN.DataSource = d;

        gvResultsByISBN.PageIndex = e.NewPageIndex;
        gvResultsByISBN.DataBind();
    }

    //display selected book from keyword results - Elizabeth Dudding
    protected void gvResultsByKeyword_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Response.Redirect("BookDetails.aspx?id=" + libID + "&barcode=" + gvResultsByKeyword.SelectedDataKey.Value.ToString());
        code = gvResultsByKeyword.SelectedDataKey.Value.ToString();
        getDetails(libID, code);
    }

    //display selected book from title results - Elizabeth Dudding
    protected void gvResultsByTitle_SelectedIndexChanged(object sender, EventArgs e)
    {

        //Response.Redirect("BookDetails.aspx?id=" + libID + "&barcode=" + gvResultsByTitle.SelectedDataKey.Value.ToString());
        code = gvResultsByTitle.SelectedDataKey.Value.ToString();
        getDetails(libID, code);
    }

    //display selected book from author results - Elizabeth Dudding
    protected void gvResultsByAuthor_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Response.Redirect("BookDetails.aspx?id=" + libID + "&barcode=" + gvResultsByAuthor.SelectedDataKey.Value.ToString());
        code = gvResultsByAuthor.SelectedDataKey.Value.ToString();
        getDetails(libID, code);
    }

    //display selected book from subject results - Elizabeth Dudding
    protected void gvResultsBySubject_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Response.Redirect("BookDetails.aspx?id=" + libID + "&barcode=" + gvResultsBySubject.SelectedDataKey.Value.ToString());
        code = gvResultsBySubject.SelectedDataKey.Value.ToString();
        getDetails(libID, code);
    }

    //display selected book from isbn results - Elizabeth Dudding
    protected void gvResultsByISBN_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Response.Redirect("BookDetails.aspx?id=" + libID + "&barcode=" + gvResultsByISBN.SelectedDataKey.Value.ToString());
        code = gvResultsByISBN.SelectedDataKey.Value.ToString();
        getDetails(libID, code);
    }

    //view general book details such as title, author, call #, and availability - Elizabeth Dudding
    protected void btnCopyDetails_Click(object sender, EventArgs e)
    {
        btnMoreDetails.CssClass = "notSelected";
        btnCopyDetails.CssClass = "selected";
        pnlBookDetails.Visible = true;
        pnlMoreDetails.Visible = false;
        pnlSimpleDetails.Visible = true;
    }

    //view book details such as series title, isbn, and subject - Elizabeth Dudding
    protected void btnMoreDetails_Click(object sender, EventArgs e)
    {
        btnCopyDetails.CssClass = "notSelected";
        btnMoreDetails.CssClass = "selected";
        pnlBookDetails.Visible = true;
        pnlSimpleDetails.Visible = false;
        pnlMoreDetails.Visible = true;
    }

    //get the book details - Elizabeth Dudding
    private void getDetails(int lib, string code)
    {
        try
        {
            library = manageLibrary.getLibrary(lib.ToString());
            book = manageBook.getBookDetails(code);
            publisher = managePublisher.getPublisher(book.PublisherID);
            displayDetails();
        }
        catch (Exception ex)
        {
            lblMessage.Text = "Error: " + ex.ToString();
        }
    }

    //display the book details - Elizabeth Dudding
    private void displayDetails()
    {
        pnlBookDetails.Visible = true;
        pnlMoreDetails.Visible = false;
        pnlSimpleDetails.Visible = true;
        lblTitle.Text = book.Title;
        lblAuthor.Text = book.Author;
        lblPublisher.Text = publisher.Name;
        lblCallNumber.Text = book.CallNumber;
        lblLibrary.Text = library.Name;
        lblSeriesTitle.Text = book.SeriesTitle;
        lblSubject.Text = book.Subject;
        lblKeywords.Text = book.Keywords;
        lblDatePublished.Text = book.DatePublished.ToString();
        lblIsYoungAdult.Text = book.isYoungAdult.ToString();
        lblISBN.Text = book.ISBN.ToString();
        lblBarcode.Text = book.Barcode;
        lblSuccess.Visible = false;
        gvReserveBookResults.Visible = false;
        lblPatron.Text = patron.PersonID.ToString();
        if (Convert.ToInt32(lblPatron.Text) == 0)
        {
            btnReserveBook.Enabled = false;
            lblMessage.Text = "Please log in to reserve a book.";
        }
        //if (Convert.ToInt64(lblISBN.Text) == 0)
        //    ImageButton1.Visible = false;
        getBookStatus();

    }

    //close the book details panel - Elizabeth Dudding
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        pnlBookDetails.Visible = false;
    }

    //check the status of the book - Elizabeth Dudding
    protected void getBookStatus()
    {
        if (manageCheckedOutBook.isBookCheckedOut(book.Barcode) < 1)
            lblStatus.Text = coBook.dueDate.ToShortDateString();
        else if (manageCheckedOutBook.isBookRenewable(book.Barcode) < 1)
            lblStatus.Text = reservedBook.ReserveStatus;
        else
            lblStatus.Text = "Available";
    }

    //reserve selected book - Elizabeth Dudding
    protected void btnReserveBook_Click(object sender, EventArgs e)
    {
        try
        {
            reservedBook.Barcode = lblBarcode.Text;
            reservedBook.PersonID = patron.PersonID;
            reservedBook.DateReserved = utilManager.getDate();
            reservedBook.ReserveStatus = "On Reserve";
            manageReservedBook.reserveBook(reservedBook);
            lblSuccess.Visible = true;
            lblSuccess.Text = "You have reserved: ";
            gvReserveBookResults.Visible = true;
            displayPatronReservedBooks();
            
        }
        catch (Exception ex)
        {
            lblMessage.Text = "error: " + ex.ToString();
        }
    }

    //show patron's books reserved - Elizabeth Dudding
    protected void displayPatronReservedBooks()
    {
        try
        {
            SqlDataAdapter data = manageReservedBook.getPatronBooks(patron.PersonID);
            DataSet d = new DataSet();
            data.Fill(d);

            gvReserveBookResults.DataSource = d;
            gvReserveBookResults.DataBind();

            if (gvReserveBookResults.Rows.Count < 1)
            {
                lblMessage.Text = "You have no books reserved.";
                gvReserveBookResults.Visible = false;
            }
            else
            {
                lblMessage.Text = "";
                gvReserveBookResults.Visible = true;
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = "Error: " + ex.ToString();
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("http://books.google.com/books?vid=ISBN" + book.ISBN);
    }
}
